package com.five.years.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * @author : qinhuajun
 * @Date :2020-05-04
 */
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@Setter
@Getter
@Data
public class ApiResponse<T> {

    /**
     * 0表示成功,非0为失败
     */
    private String status;
    private String info;
    private T data;
    private T obj;

    public ApiResponse() {
        status = SystemCode.SUCCESS.getCode();
        info = "success";
    }

    public ApiResponse(T data) {
        status = SystemCode.SUCCESS.getCode();
        info = "success";
        this.obj = data;
    }

    public static ApiResponse fail(String message) {
        return new ApiResponse(SystemCode.FAIL.getCode(), message, null);
    }

    public static ApiResponse fail(SystemCode systemCode) {
        return new ApiResponse(systemCode.getCode(), systemCode.getMessage(), null);
    }

    public static ApiResponse fail(String status, String message) {
        return new ApiResponse(status, message, null);
    }

    public static ApiResponse success(Object object) {
        return new ApiResponse(object);
    }

    public static ApiResponse success() {
        return new ApiResponse();
    }

    public ApiResponse(String status, String message, T data) {
        this.status = status;
        this.info = message;
        this.obj = data;
    }

}
